package com.example.administrator.capital.model;


import com.example.administrator.capital.R;
import com.example.administrator.capital.model.base.BaseModel;
import com.example.administrator.capital.model.base.IActionRequest;
import com.example.administrator.capital.model.bean.ErrorBean;
import com.example.administrator.capital.url.Url;
import com.example.administrator.capital.utils.JsonUtils;
import com.example.administrator.capital.utils.LogUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

/**
 * Created by Administrator on 2017/12/19 0019.
 * 资讯 model
 */
public class ConsultModel extends BaseModel {


    /**
     * @param iActionRequest
     * @param uid
     * 查看资讯接口
     */
    public void selectarticle(final IActionRequest<String> iActionRequest, String uid){
        OkGo.<String>post(Url.SELECTARTICLE).tag(this).params("uid",uid).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                LogUtils.d("查看资讯"+response.body());
                if (response.body().contains("\"code\":1")) {
                    iActionRequest.success(response);
                } else {
                    ErrorBean bean = JsonUtils.parse(response.body(), ErrorBean.class);
                    iActionRequest.failMsg(bean.getMsg());
                }
            }
            @Override
            public String convertResponse(okhttp3.Response response) throws Throwable {
                return super.convertResponse(response);
            }
            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                iActionRequest.code(R.string.error_msg);
            }
        });
    }

    /**
     * @param iActionRequest
     * 资讯列表接口
     */
    public void articlelist(final IActionRequest<String> iActionRequest){
        OkGo.<String>post(Url.ARTICLELIST).tag(this).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                LogUtils.d("资讯列表"+response.body());
                if (response.body().contains("\"code\":1")) {
                    iActionRequest.success(response);
                } else {
                    ErrorBean bean = JsonUtils.parse(response.body(), ErrorBean.class);
                    iActionRequest.failMsg(bean.getMsg());
                }
            }
            @Override
            public String convertResponse(okhttp3.Response response) throws Throwable {
                return super.convertResponse(response);
            }
            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                iActionRequest.code(R.string.error_msg);
            }
        });
    }


    /**
     * @param iActionRequest
     * @param id
     * 资讯详情html页面
     */
    public void article(final IActionRequest<String> iActionRequest,String id){
        OkGo.<String>post(Url.ARTICLE).tag(this).params("id",id).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                LogUtils.d("资讯详情html"+response.body());
                if (response.body().contains("\"code\":1")) {
                    iActionRequest.success(response);
                } else {
                    ErrorBean bean = JsonUtils.parse(response.body(), ErrorBean.class);
                    iActionRequest.failMsg(bean.getMsg());
                }
            }
            @Override
            public String convertResponse(okhttp3.Response response) throws Throwable {
                return super.convertResponse(response);
            }
            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                iActionRequest.code(R.string.error_msg);
            }
        });
    }
}
